//
//  MainViewController.m
//  projectA_kuaiZhui
//
//  Created by 张祎 on 15-3-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MainViewController.h"
#import "MainCollectionViewCell.h"
#import "UIButton+InitButton.h"
#import "Text1ViewController.h"
#import "UIView+ChangeFrame.h"
#import "AFNetworking.h"
#import "NetHandler.h"
#import "galleryItem.h"
@interface MainViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain) UIImageView *blockView;
@property(nonatomic, retain) UIImageView *naviView;
@property(nonatomic, retain) UIImageView *spotView;
@property(nonatomic, retain) UIButton *recommendPageButton;
@property(nonatomic, retain) UIButton *classifyPageButton;
@property(nonatomic, retain) UIButton *updatePageButton;
@property(nonatomic, retain) UIButton *searchPageButton;
@property(nonatomic, retain) UIButton *rankingPageButton;
@property(nonatomic, retain) UICollectionView *mainCollectionView;
@property(nonatomic, retain) NSMutableArray *galleryItems;
@end
@implementation MainViewController
- (void)dealloc
{
    [_naviView release];
    [_recommendPageButton release];
    [_classifyPageButton release];
    [_updatePageButton release];
    [_searchPageButton release];
    [_rankingPageButton release];
    [super dealloc];
}

//隐藏首页navigationBar
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)loadData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSString *str = [@"http://app.u17.com/v3/app/ios/phone/recommend/itemlist?version=10.0.6.1&deviceId=1efd60c928005fb3397278b7a37d6a9bbeac5022&model=iPhone 4S&time=1426754265&" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"请求成功%@", responseObject);
//                NSLog(@"请求成功%@", [responseObject objectForKey:@"theme_list"]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
    }];
    //判断网络状态
    AFNetworkReachabilityManager *manager1 = [AFNetworkReachabilityManager  sharedManager];
    [manager1 setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
    }];
    
}
- (void)netHandel
{
    NSString *str = @"http://app.u17.com/v3/app/ios/phone/recommend/itemlist?version=10.0.6.1&deviceId=1efd60c928005fb3397278b7a37d6a9bbeac5022&model=iPhone 4S&time=1426754265&";
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        NSError *error = nil;
        id target = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSLog(@"%@",target);
        self.galleryItems = [NSMutableArray array];
        
        //解析首页幻灯片数据
        NSMutableArray *galleryArr = [[[[[target objectForKey:@"data"] objectForKey:@"returnData"] objectForKey:@"dataList"] objectAtIndex:0] objectForKey:@"galleryItems"];
        for (NSMutableDictionary *dic in galleryArr) {
            GalleryItem *gallery = [[GalleryItem alloc]initWithDic:dic];
            [self.galleryItems addObject:gallery];
            [gallery release];
        }
        
        [self createView];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //    [self loadData];
    [self netHandel];
    
    
}
//创建视图
- (void)createView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationController.navigationBar.translucent = YES;
    CGFloat width = self.view.bounds.size.width;
    CGFloat height = self.view.bounds.size.height;
    
    self.naviView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, 64)];
    _naviView.image = [UIImage imageNamed:@"navi_bg_1.jpg"];
    _naviView.userInteractionEnabled = YES;
    _naviView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_naviView];
    [_naviView release];
    CGFloat naviWidth = _naviView.frame.size.width;
    CGFloat naviHeight = _naviView.frame.size.height;
    
    
    
    //共5个button, 用来表示 推荐 分类 最新 找找 排行
    self.recommendPageButton = [UIButton buttonWithTitle:@"推荐" frame:CGRectMake(0, 10, naviWidth / 5, naviHeight) target:self action:@selector(naviButtonAction:)];
    _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:20];
    _recommendPageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_naviView addSubview:_recommendPageButton];
    
    self.classifyPageButton = [UIButton buttonWithTitle:@"分类" frame:CGRectMake(naviWidth / 5, 10, naviWidth / 5, naviHeight) target:self action:@selector(naviButtonAction:)];
    _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    _classifyPageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_naviView addSubview:_classifyPageButton];
    
    self.updatePageButton = [UIButton buttonWithTitle:@"最新" frame:CGRectMake(naviWidth * 2 / 5, 10, naviWidth / 5, naviHeight) target:self action:@selector(naviButtonAction:)];
    _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    _updatePageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_naviView addSubview:_updatePageButton];
    
    self.searchPageButton = [UIButton buttonWithTitle:@"找找" frame:CGRectMake(naviWidth * 3 / 5, 10, naviWidth / 5, naviHeight) target:self action:@selector(naviButtonAction:)];
    _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    _searchPageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_naviView addSubview:_searchPageButton];
    
    self.rankingPageButton = [UIButton buttonWithTitle:@"排行" frame:CGRectMake(naviWidth * 4 / 5, 10, naviWidth / 5, naviHeight) target:self action:@selector(naviButtonAction:)];
    _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    _rankingPageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_naviView addSubview:_rankingPageButton];
    
    
#warning 主页最大的collectionView
    UICollectionViewFlowLayout *mainFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    mainFlowLayout.itemSize = CGSizeMake(width, height - 64 - 49);
    mainFlowLayout.minimumInteritemSpacing = 0;
    mainFlowLayout.minimumLineSpacing = 0;
    mainFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, width, height - 64 - 49) collectionViewLayout:mainFlowLayout];
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
    _mainCollectionView.bounces = NO;
    _mainCollectionView.pagingEnabled = YES;
    //_collectionView的默认颜色为黑色
    
    _mainCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mainCollectionView];
    [_mainCollectionView release];
    
    //小白点
    self.spotView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 52, 10, 10)];
    _spotView.backgroundColor = [UIColor whiteColor];
    //    _spotView.layer.cornerRadius = 100;
    //    _spotView.clipsToBounds = YES;
    [_naviView addSubview:_spotView];
    [_spotView release];
    
    
    //    self.tabBarController.tabBar.hidden = YES;
}


#pragma - mark UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.row == 0) {
        [_mainCollectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:@"a"];
        MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"a" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor cyanColor];
        cell.galleryItems = _galleryItems;
        return cell;
    }
    else if (indexPath.row == 1) {
        [_mainCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"b"];
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"b" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor orangeColor];
        return cell;
    }
    else if (indexPath.row == 2) {
        [_mainCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"c"];
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"c" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor blackColor];
        return cell;
    }
    else if (indexPath.row == 3) {
        [_mainCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"d"];
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"d" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor greenColor];
        return cell;
    }
    else{
        [_mainCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"e"];
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"e" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor cyanColor];
        return cell;
    }
    
}




#pragma - mark 实现naviView的功能
- (void)naviButtonAction:(UIButton *)button
{
    //实现button的跳转页面功能
    if (button == _recommendPageButton) {
        _mainCollectionView.contentOffset = CGPointMake(0, 0);
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    if (button == _classifyPageButton) {
        _mainCollectionView.contentOffset = CGPointMake(self.view.bounds.size.width, 0);
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    if (button == _updatePageButton) {
        _mainCollectionView.contentOffset = CGPointMake(self.view.bounds.size.width * 2, 0);
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    if (button == _searchPageButton) {
        _mainCollectionView.contentOffset = CGPointMake(self.view.bounds.size.width * 3, 0);
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    if (button == _rankingPageButton) {
        _mainCollectionView.contentOffset = CGPointMake(self.view.bounds.size.width * 4, 0);
        button.titleLabel.font = [UIFont systemFontOfSize:22];
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //设置小白点
    [_spotView changeFrameX:scrollView.contentOffset.x / 5 + 30];
    NSLog(@"%lf",scrollView.contentOffset.x);
    NSLog(@"%lf",scrollView.contentOffset.x / 5 + 30 );
    //实现button字体的放大缩小
    if (scrollView.contentOffset.x < self.view.bounds.size.width / 5) {
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:20];
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    if (scrollView.contentOffset.x >= self.view.bounds.size.width) {
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:20];
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    if (scrollView.contentOffset.x >= self.view.bounds.size.width * 2) {
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:20];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    if (scrollView.contentOffset.x >= self.view.bounds.size.width * 3) {
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:18];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    if (scrollView.contentOffset.x >= self.view.bounds.size.width * 3) {
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:20];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    if (scrollView.contentOffset.x >= self.view.bounds.size.width * 4) {
        _recommendPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _classifyPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _updatePageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _searchPageButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _rankingPageButton.titleLabel.font = [UIFont systemFontOfSize:20];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
